package com.visitor.web.controller;

import com.github.pagehelper.PageInfo;
import com.visitor.bean.Admin;
import com.visitor.service.AdminService;
import com.visitor.web.vm.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Api(tags="管理员")
@Controller
@RequestMapping(value = "adminManager")
public class AdminController {
	@Autowired
	private AdminService adminService;


	@ApiOperation(value="添加子管理员",notes="添加子管理员")
	@ResponseBody
	@PostMapping("/addAdmin")
	public Response<String> addAdmin(Admin admin){
		return adminService.saveOrUpdate(admin);
	}

	@ApiOperation(value="按照编号删除子管理员信息",notes="按照编号删除子管理员信息")
	@ApiImplicitParam(name="id",value="子管理员编号",paramType="path",required=true)
	@ResponseBody
	@PostMapping("/delAdmin/{id}")
	public Response<String> delAdmin(@PathVariable("id")Long id){
		adminService.deleteUserById(id);
		return Response.ok("success");
	}

	@ApiOperation(value="按照编号删除子管理员信息",notes="按照编号删除子管理员信息")
	@ApiImplicitParam(name="id",value="子管理员编号",paramType="path",required=true)
	@ResponseBody
	@PostMapping("/updateAdmin")
	public Response<String> updateAdmin(Admin admin, HttpSession session) {
		session.setAttribute("loginAdmin",admin);
		return adminService.saveOrUpdate(admin);
	}

	@ApiOperation(value="分页查找所有子管理员",notes="分页查找所有子管理员")
	@ApiImplicitParams({
		@ApiImplicitParam(name="pageNum",value="当前页码",required=false,defaultValue="1"),
		@ApiImplicitParam(name="pageSize",value="每页显示数据个数(0是全部)",required=false,defaultValue="2")
	})
	@GetMapping("/adminList")
	public ModelAndView adminList(@RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
								  @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,Admin  admin,ModelAndView modelAndView) {
		PageInfo<Admin> data= adminService.getAllByPage(pageNum, pageSize,admin);
		modelAndView.addObject("page",data);
		modelAndView.setViewName("admin/admin-list");
		return modelAndView;
	}


	//跳转添加子管理员页面
	@GetMapping("/admin-add")
	public ModelAndView adminAddHtml(ModelAndView modelAndView) {
		modelAndView.setViewName("admin/admin-add");
		return modelAndView;
	}	//跳转添加子管理员页面
	@GetMapping("/admin-edit")
	public ModelAndView adminEditHtml(Long id,ModelAndView modelAndView) {
		Admin admin=adminService.findUserById(id);
		modelAndView.addObject("admin",admin);
		modelAndView.setViewName("admin/admin-edit");
		return modelAndView;
	}

	//个人信息修改
	@GetMapping("/admin-update")
	public ModelAndView adminUpdateHtml(Long id,ModelAndView modelAndView) {
		Admin admin=adminService.findUserById(id);
		modelAndView.addObject("admin",admin);
		modelAndView.setViewName("admin/admin-update");
		return modelAndView;
	}


}
